<%@page import="com.ddd.entity.Commision"%>
<%@page import="com.ddd.context.Context"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.ddd.service.CommisionService"%>
<%@page import="com.ddd.context.Parameters"%>
<%@page import="com.ddd.util.Util"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    Util.checkBEAuthorized(request, response);

    String message = request.getParameter("message");
    String gotoPage = request.getParameter("gotoPage");

    Parameters para = new Parameters(request);
    if (Util.isNotBlank(gotoPage)) {
        para.setCurrentPage(Integer.parseInt(gotoPage));
    } else {
        para.setCurrentPage(1);
    }

    // load data
    CommisionService commisionService = new CommisionService(request);
    ArrayList commisionList = commisionService.loadAll();

%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=Context.SITE_NAME%></title>

        <link type="text/css" href="css/style.css" rel="stylesheet" />
        <link type="text/css" href="css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
        <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
        <script type="text/javascript">
            function doAdd(){
                var year = $('#year_new').val();
                var amount = $('#amount_new').val();
                var currcommision = $('#currcommision_new').val();
                var postcommision = $('#postcommision_new').val();
                
                $( "#dialog_add" ).dialog({
                    title: "新增回饋金",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "新增": function() {
                            $( this ).dialog( "close" );
                            window.location = "../doCommision?mode=add&year=" + year + "&amount=" + amount + "&currCommision=" +  currcommision + "&postCommision=" + postcommision;
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });  
            }
       
            function doUpdate (i){
                var tmpName = '#year_' + i;
                var year = $(tmpName).val();
                
                tmpName = '#amount_' + i;
                var amount = $(tmpName).val();
                
                tmpName = '#currcommision_' + i;
                var currcommision = $(tmpName).val();
                
                tmpName = '#postcommision_' + i;
                var postcommision = $(tmpName).val();
                
                tmpName = '#id_' + i;
                var id = $(tmpName).val();
                
                $( "#dialog_update" ).dialog({
                    title: "修改回饋金",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "修改": function() {
                            $( this ).dialog( "close" );
                            window.location = "../doCommision?mode=update&id=" + id + "&year=" + year + "&amount=" + amount + "&currCommision=" +  currcommision + "&postCommision=" + postcommision;
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });  
            }
            
            function doDelete (id){
                $( "#dialog_delete" ).dialog({
                    title: "刪除回饋金",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "刪除": function() {
                            $( this ).dialog( "close" );
                            window.location = "../doCommision?mode=delete&id=" + id;
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }
            
            function gotoPage(page){
                window.location = "commision.jsp?gotoPage=" + page;
            }
            
            function showMessage(){
            <%if (Util.isNotBlank(message)) {%>
                    $( "#dialog_message" ).dialog({
                        modal: true,
                        title:"訊息",
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
            <%}%>
                }
            
        </script>
    </head>
    <body onload="showMessage();">
        <div id="wrapper">
            <div id="header">
                <div id="logo">
                    <h1><a href="#"><%=Context.SITE_NAME%></a></h1>
                </div>
            </div>
            <!-- end #header -->
            <div id="menu">
                <%@ include file="menu.jsp" %>
            </div>
            <!-- end #menu -->
            <div id="page">
                <div id="page-bgtop">
                    <div id="page-bgbtm">
                        <!-- starrt #content -->
                        <div id="content">
                            <table>
                                <thead>
                                    <tr>
                                        <th>序號（不可修改）</th>
                                        <th>年度（不可修改）</th>
                                        <th>總金額</th>
                                        <th>現刊%</th>
                                        <th>過刊％</th>
                                        <th>&nbsp;</th>
                                        <th>&nbsp;</th>
                                    </tr>
                                </thead>
                                <%
                                    int left = para.getPageSize();
                                    for (int i = (para.getCurrentPage() - 1) * para.getPageSize(); i < para.getCurrentPage() * para.getPageSize() && i < para.getTotalSize(); i++) {
                                        left--;
                                        Commision c = (Commision) commisionList.get(i);
                                %>
                                <tr>
                                    <td><input type="text" id="id_<%=i%>" name="id_<%=i%>" value="<%=c.getId()%>" readonly></input></td>
                                    <td><input type="text" id="year_<%=i%>" name="year_<%=i%>" value="<%=c.getYear()%>" readonly></input></td>
                                    <td><input type="text" id="amount_<%=i%>" name="amount_<%=i%>" value="<%=c.getAmount()%>"></input></td>
                                    <td><input type="text" id="currcommision_<%=i%>" name="currcommision_<%=i%>" value="<%=c.getCurrCommision()%>"></input></td>
                                    <td><input type="text" id="postcommision_<%=i%>" name="postcommision_<%=i%>" value="<%=c.getPostCommision()%>"></input></td>
                                    <td><a href='javascript:doUpdate("<%=i%>");'>修改</a></td>
                                    <td><a href='javascript:doDelete("<%=c.getId()%>");'>刪除</a></td>
                                </tr>
                                <%}%>

                                <!-- 新增 -->
                                <tr>
                                    <td>&nbsp;</td>
                                    <td><input type="text" id="year_new" name="year_new"></input></td>
                                    <td><input type="text" id="amount_new" name="amount_new"></input></td>
                                    <td><input type="text" id="currcommision_new" name="currcommision_new"></input></td>
                                    <td><input type="text" id="postcommision_new" name="postcommision_new"></input></td>
                                    <td>&nbsp;</td>
                                    <td><a href='javascript:doAdd();'>新增</a></td>
                                </tr>

                                <%for (int i = 0; i < left; i++) {%>
                                <tr>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                </tr>
                                <%}%>
                            </table>
                            <br /> 
                            <p align="center">
                                <%=para.getCurrentPage()%> / <%=para.getTotalPage()%> (總筆數: <%=para.getTotalSize()%>)
                            </p>
                            <p align="center">
                                <%if (para.getCurrentPage() > 1) {%>
                                <input type="button" value="上一頁" onclick='gotoPage(<%=para.getCurrentPage() - 1%>)'>
                                <%}%>
                                <%if (para.getCurrentPage() < para.getTotalPage()) {%>
                                <input type="button" value="下一頁" onclick='gotoPage(<%=para.getCurrentPage() + 1%>)'>
                                <%}%>
                            </p>

                        </div>
                        <!-- end #content -->

                        <!-- end #sidebar -->
                        <div style="clear: both;">&nbsp;</div>
                    </div>
                </div>
            </div>
            <!-- end #page -->
        </div>
        <div id="footer">
            <p><%=Context.FOOTER%></p>
        </div>
        <!-- end #footer -->
        <!-- ui-dialog -->
        <div id="dialog_add" style="display: none">
            <p>是否要新增回饋金？</p>
        </div>

        <div id="dialog_update" style="display: none">
            <p>是否要修改回饋金？</p>
        </div>

        <div id="dialog_delete" style="display: none">
            <p>是否要刪除回饋金？</p>
        </div>

        <div id="dialog_message" style="display: none;">
            <p><%=message%></p>
        </div>

    </body>
</html>
